//
//  FirstViewController.swift
//  CoreImageDemo
//
//  Created by wei.feng on 2019/11/29.
//  Copyright © 2019 weifeng. All rights reserved.
//

import UIKit

class FirstViewController: UITableViewController {
    
    lazy var dataSource : [String] = {
        return ImageFilterStore.allCICategories
    }();
    
    var isCategory : Bool = {
        return true
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
            cell?.textLabel?.font = UIFont.systemFont(ofSize: 16)
            cell?.detailTextLabel?.font = UIFont.systemFont(ofSize: 12)
            cell?.textLabel?.textColor = UIColor.black;
            cell?.detailTextLabel?.textColor = UIColor.lightGray
        }
        
        let name = self.dataSource[indexPath.row]
        cell?.detailTextLabel?.text = name
        cell?.textLabel?.text = isCategory ? CIFilter.localizedName(forCategory: name) : CIFilter.localizedName(forFilterName: name)
        
        return cell!
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        let categoryname = self.dataSource[indexPath.row]
        if self.isCategory {
            let vc = FirstViewController()
            vc.dataSource = CIFilter.filterNames(inCategory: categoryname)
            vc.isCategory = false
            self.navigationController?.pushViewController(vc, animated: true)
        }else{
            let vc = DisplayDetailViewController()
            vc.filterName = categoryname
            vc.inputImage = UIImage(named: "14271");
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}

